package org.phantom.securityframework.local;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.phantom.securityframework.api.GroupManager;
import org.phantom.securityframework.api.Permission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.phantom.securityframework.api.datapermission.DataDimension;
import org.phantom.securityframework.api.datapermission.DimensionType;
import org.phantom.securityframework.api.domain.Account;
import org.phantom.securityframework.api.domain.Group;
import org.phantom.securityframework.api.domain.Role;
import org.phantom.securityframework.api.util.AuthUtil;

@Service
@Transactional
public class GroupManagerLocalImpl implements GroupManager{

	@Resource
	private SqlSession session;


	public void relAccountGroup(Serializable accountid, Serializable groupIds[]){
		if(accountid==null){
			throw new RuntimeException("accountId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Serializable g:groupIds) {
			params.put("accountId",(Integer) accountid);
			params.put("groupId",(Integer) g);
			session.insert("org.phantom.securityframework.GroupMapper.relAccountGroup",params);
		}
		
	}
	@Override
	public void relAccountGroup(Account account, Group[] groups) {
		if(account==null){
			throw new RuntimeException("accountId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Group g:groups) {
			params.put("accountId",account.getId());
			params.put("groupId", g.getId());
			session.insert("org.phantom.securityframework.GroupMapper.relAccountGroup",params);
		}
	}
	
	@Override
	public void relAccountGroup(Account account, Serializable[] groupIds) {
		if(account==null){
			throw new RuntimeException("accountId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Serializable s : groupIds) {
			params.put("accountId",account.getId());
			params.put("groupId", (Integer) s);
			session.insert("org.phantom.securityframework.GroupMapper.relAccountGroup",params);
		}
	}
	
	
	

	@Override
	public void unrelAccountGroup(Serializable accountId,
			Serializable[] groupIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		for (Serializable groupId : groupIds) {
			params.put("accountId", accountId);
			params.put("groupId", groupId);
			session.delete("org.phantom.securityframework.GroupMapper.unrelAccountGroup",params);
			params.clear();
		}
	}

	public void relAccountGroup(Group group,Account[] accounts){
		Map<String, Object> params = new HashMap<String, Object>();
		for (Account a: accounts) {
			params.put("accountId", a.getId());
			params.put("groupId", group.getId());
			session.delete("org.phantom.securityframework.GroupMapper.relAccountGroup",params);
			params.clear();
		}
	}
	public void unrelAccountGroup(Group group,Account[] accounts){
		Map<String, Object> params = new HashMap<String, Object>();
		for (Account a: accounts) {
			params.put("accountId", a.getId());
			params.put("groupId", group.getId());
			session.delete("org.phantom.securityframework.GroupMapper.unrelAccountGroup",params);
			params.clear();
		}
	}
	
	
	@Override
	public void relRoleGroup(Role role, Group[] groups) {
		if(role==null){
			throw new RuntimeException("roleId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Group g : groups) {
			params.put("roleId",role.getId());
			params.put("groupId", g.getId());
			session.insert("org.phantom.securityframework.GroupMapper.relRoleGroup",params);
		}
		
	}

	@Override
	public void relRoleGroup(Role role, Serializable[] groupIds) {
		if(role==null){
			throw new RuntimeException("roleId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Serializable id : groupIds) {
			params.put("roleId",role.getId());
			params.put("groupId", (Integer) id);
			session.insert("org.phantom.securityframework.GroupMapper.relRoleGroup",params);
		}
		
		
	}

	@Override
	public void relRoleGroup(Serializable roleId, Serializable[] groupIds) {

		if(roleId==null){
			throw new RuntimeException("roleId can not be null!");
		}
		Map<String,Integer> params = new HashMap<String,Integer>();
		for (Serializable id : groupIds) {
			params.put("roleId",(Integer) roleId);
			params.put("groupId", (Integer) id);
			session.insert("org.phantom.securityframework.GroupMapper.relRoleGroup",params);
		}
	}

	@Override
	public Group add(Group group) {
		if(group==null) return null;
		session.insert("org.phantom.securityframework.GroupMapper.saveGroup",group);
		return group;
	}

	@Override
	public Group modify(Group group) {
		if(group.getId()==null || group.getId().intValue()==0){
			throw new RuntimeException("id can not be null");
		}
		session.update("org.phantom.securityframework.GroupMapper.modifyGroup",group);
		return group;
	}

	@Override
	public Group remove(Group group) {
		if(group.getId()==null || group.getId().intValue()==0){
			throw new RuntimeException("id can not ne null!");
		}
		session.delete("org.phantom.securityframework.GroupMapper.removeGroup",group);
		return null;
	}

	@Override
	public Group remove(Integer id) {
		return this.remove(this.get(id));
	}

	@Override
	public Group get(Integer id) {
		if(id==null || id.intValue()==0)return null;
		return (Group) session.selectOne("org.phantom.securityframework.GroupMapper.get",id);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Group> list() {
		Map<String, Object> params = new HashMap<String, Object>();
		if(!params.containsKey("platformId")){
			params.remove("platformId");
			params.put("platformId", AuthUtil.getPlatformId());
		}
		return session.selectList("org.phantom.securityframework.GroupMapper.list",params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Group> list(Map<String, Object> params) {
		if(params==null || params.size()<=0)
			return this.list();
	
		if(!params.containsKey("platformId")){
			params.remove("platformId");
			params.put("platformId", AuthUtil.getPlatformId());
		}
		return session.selectList("org.phantom.securityframework.GroupMapper.list",params);
	}

	@Override
	public void unrelRoleGroup(Serializable groupId,Serializable[] roleIds){
		Map<String, Object> params = new HashMap<String, Object>();
		for (Serializable roleId : roleIds) {
			params.put("groupId", groupId);
			params.put("roleId", roleId);
			session.delete("org.phantom.securityframework.GroupMapper.unrelRoleGroup",params);
			params.clear();
		}
		
	}

	@Override
	public Integer count() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("platformId", AuthUtil.getPlatformId());
		return (Integer) session.selectOne("org.phantom.securityframework.GroupMapper.count");
	}

	@Override
	public Integer count(Map<String, Object> params) {
		if(params==null || params.size()<=0){
			return this.count();
		}
		if(params.containsKey("platformId")){
			params.remove("platformId");
		}
		params.put("platformId", AuthUtil.getPlatformId());
		return (Integer) session.selectOne("org.phantom.securityframework.GroupMapper.count",params);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Permission> permissions(Group group) {
		return session.selectList("org.phantom.securityframework.GroupMapper.listGroupPermission",group);
	}
	@Override
	public void relGroupDataDimension(Group group, DataDimension dataDimension) {
		// TODO Auto-generated method stub
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public DataDimension getDataDimension(Group g) {
		List<DimensionType> types = session.selectList("org.phantom.securityframework.GroupMapper.listDataDimension",g.getId());
		DataDimension d = new DataDimension();
		d.setDimensionType(types.toArray(new DimensionType[]{}));
		return d;
	}

}
